#!/bin/bash

# Nginx with Docker 部署脚本
echo ==================================================
echo Script startup for deploy Nginx
echo ==================================================

# Nginx镜像库地址
NGINX_DOCKER_DOCKER_RESISTRY=daocloud.io/library/nginx
# Nginx镜像版本
NGINX_DOCKER_VERSION=latest
# Nginx的Docker实例名称
NGINX_DOCKER_INSTANCE_NAME=nginx-main
# Nginx物理机映射HTTP端口
NGINX_PHYSICAL_MACHINE_HTTP_PORT=80
# Nginx虚拟机映射HTTP端口
NGINX_VIRTUAL_MACHINE_HTTP_PORT=80
# Nginx物理机映射HTTPS端口
NGINX_PHYSICAL_MACHINE_HTTPS_PORT=443
# Nginx虚拟机映射HTTPS端口
NGINX_VIRTUAL_MACHINE_HTTPS_PORT=443
# Nginx物理机中配置文件路径
NGINX_PHYSICAL_MACHINE_CONF_PATH=/home/nginx/conf/nginx.conf
# Nginx虚拟机中配置文件路径
NGINX_VIRTUAL_MACHINE_CONF_PATH=/etc/nginx/nginx.conf
# Nginx物理机中网站目录路径
NGINX_PHYSICAL_MACHINE_WWW_PATH=/home/nginx/www
# Nginx虚拟机中网站目录路径
NGINX_VIRTUAL_MACHINE_WWW_PATH=/opt/nginx/www
# Nginx物理机中日志路径
NGINX_PHYSICAL_MACHINE_LOG_PATH=/home/nginx/log
# Nginx虚拟机中日志路径
NGINX_VIRTUAL_MACHINE_LOG_PATH=/opt/nginx/log
# Nginx物理机中MIME.TYPE文件路径
NGINX_PHYSICAL_MACHINE_MIMETYPE_PATH=/home/nginx/conf/mime.types
# Nginx虚拟机中MIME.TYPE文件路径
NGINX_VIRTUAL_MACHINE_MIMETYPE_PATH=/etc/nginx/mime.types
# Nginx物理机中pid文件路径
NGINX_PHYSICAL_MACHINE_PID_PATH=/home/nginx/nginx.pid
# Nginx虚拟机中pid文件路径
NGINX_VIRTUAL_MACHINE_PID_PATH=/var/run/nginx.pid
# Nginx物理机中证书文件路径
NGINX_PHYSICAL_MACHINE_SSL_CERTS_PATH=/home/documents/ssl_certificates
# Nginx虚拟机中证书文件路径
NGINX_VIRTUAL_MACHINE_SSL_CERTS_PATH=/etc/nginx/certs
# Nginx物理机中文件服务器路径
NGINX_PHYSICAL_MACHINE_HTTP_SERVER_PATH=/home/storage/file_system
# Nginx虚拟机中文件服务器路径
NGINX_VIRTUAL_MACHINE_HTTP_SERVER_PATH=/home
# Nginx物理机中文件服务器路径
NGINX_PHYSICAL_MACHINE_SJBLOG_WEB_APP_PATH=/home/stevejrong-blog-web-app/web-app/SJBlog
# Nginx虚拟机中文件服务器路径
NGINX_VIRTUAL_MACHINE_SJBLOG_WEB_APP_PATH=/etc/nginx/app-proxy

############################ installing start ############################
echo -e "-------------------- installing\n"

echo "-------------------- pulling nginx docker image"
docker pull $NGINX_DOCKER_DOCKER_RESISTRY:$NGINX_DOCKER_VERSION
echo -e "-------------------- pull success.\n"
sleep 3

echo "-------------------- running nginx"
docker run
 -d -p
 $NGINX_PHYSICAL_MACHINE_HTTP_PORT:$NGINX_VIRTUAL_MACHINE_HTTP_PORT
 -p
 $NGINX_PHYSICAL_MACHINE_HTTPS_PORT:$NGINX_VIRTUAL_MACHINE_HTTPS_PORT
 --name
 $NGINX_DOCKER_INSTANCE_NAME
 --restart always
 --privileged=true
 -v $NGINX_PHYSICAL_MACHINE_CONF_PATH:$NGINX_VIRTUAL_MACHINE_CONF_PATH:rw
 -v $NGINX_PHYSICAL_MACHINE_WWW_PATH:$NGINX_VIRTUAL_MACHINE_WWW_PATH:rw
 -v $NGINX_PHYSICAL_MACHINE_LOG_PATH:$NGINX_VIRTUAL_MACHINE_LOG_PATH:rw
 -v $NGINX_PHYSICAL_MACHINE_MIMETYPE_PATH:$NGINX_VIRTUAL_MACHINE_MIMETYPE_PATH:rw
 -v $NGINX_PHYSICAL_MACHINE_PID_PATH:$NGINX_VIRTUAL_MACHINE_PID_PATH:rw
 -v $NGINX_PHYSICAL_MACHINE_SSL_CERTS_PATH:$NGINX_VIRTUAL_MACHINE_SSL_CERTS_PATH:rw
 -v $NGINX_PHYSICAL_MACHINE_HTTP_SERVER_PATH:$NGINX_VIRTUAL_MACHINE_HTTP_SERVER_PATH:rw
 -v $NGINX_PHYSICAL_MACHINE_SJBLOG_WEB_APP_PATH:$NGINX_VIRTUAL_MACHINE_SJBLOG_WEB_APP_PATH:rw
 $NGINX_DOCKER_DOCKER_RESISTRY:$NGINX_DOCKER_VERSION
echo -e "-------------------- run success.\n"
sleep 3

echo -e "-------------------- install completed!\n"
############################ installing end ############################

############################ validating start ############################
echo -e "-------------------- validating\n"

echo -e "-------------------- validating nginx run status\n"
docker ps -a | grep "nginx"
sleep 3

echo -e "-------------------- validate completed!\n"
############################ validating end ############################

echo =================================================
echo Deploy Nginx completed!
echo =================================================